package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.ShopCarItem;
import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Order;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.*;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import javafx.beans.binding.ObjectExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ITravelService travelService;
    @Autowired
    private ISystemMessageService systemMessageService;


    @Autowired
    private IMyCollectionService myCollectionService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private ILetterService letterService;
    @Autowired
    private ITravelLikeService travelLikeService;
    @Autowired
    private ITravelCommentService travelCommentService;
    @Autowired
    private IShopCarService shopCarService;
    @Autowired
    private IOrderService orderService;

    /**
     * 注册
     *
     * @param user
     * @return
     */
    @PostMapping
    public JsonResult register(User user) {
        JsonResult result = new JsonResult();
        try {
            userService.save(user);
        } catch (Exception e) {
            result.mark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @PutMapping("/{id}")
    public JsonResult update(User user) {
        JsonResult result = new JsonResult();
        try {
            userService.update(user);
            result.setResult(user);//设置当前用户,传给前台
        } catch (Exception e) {
            result.mark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据用户id查询游记列表
     *
     * @param qo
     * @return
     */
    @GetMapping("/{authorId}/travels")
    public PageInfo query(TravelQueryObject qo) {
        qo.setOrderBy("t.lastUpdateTime desc");
        PageInfo<?> pageInfo = travelService.queryForList(qo);
        return pageInfo;
    }

    /**
     * 根据用户id查询用户的收藏
     *
     * @param qo
     * @return
     */
    @GetMapping("/{userId}/myCollections")
    public PageInfo queryForCollection(MyCollectionQueryObject qo) {
        qo.setOrderBy("mc.collectTime desc");

        PageInfo<?> pageInfo = myCollectionService.queryForList(qo);
        return pageInfo;
    }

    @GetMapping("/{authorId}/travelsCount")
    public int count(@PathVariable Long authorId) {

        return travelService.queryForCount(authorId);
    }


    /**
     * 根据用户id查询信息
     *
     * @param id 用户id
     * @return
     */
    @GetMapping("/{id}")
    public JsonResult query(@PathVariable Long id) {
        JsonResult result = new JsonResult();
        User user = userService.getUser(id);
        if (user.getFans() == null) {
            user.setFans(0);
        }
        if (user.getFollowers() == null) {
            user.setFollowers(0);
        }
        result.setResult(user);
        return result;
    }

    /**
     * 查询是否关注
     *
     * @param currentId 当前登录用户id
     * @param userId    页面用户id
     * @return
     */
    @GetMapping("/{currentId}/{userId}")
    public boolean isFollower(@PathVariable("currentId") Long currentId, @PathVariable("userId") Long userId) {
        return userService.isFollower(currentId, userId);
    }

    /**
     * 添加关注关系
     *
     * @param currentId 当前登录用户id
     * @param userId    页面用户id
     */
    @PostMapping("/{currentId}/{userId}")
    public void saveRelation(@PathVariable("currentId") Long currentId, @PathVariable("userId") Long userId) {
        User currentUser = userService.getUser(currentId);
        currentUser.setFollowers(currentUser.getFollowers() + 1);//点击关注,登录用户的关注数+1
        userService.updateFansAndFollowersCount(currentUser);//更新数据

        User pageUser = userService.getUser(userId);
        pageUser.setFans(pageUser.getFans() + 1);//点击关注,页面用户的粉丝数+1
        userService.updateFansAndFollowersCount(pageUser);

        userService.saveRelation(currentId, userId);
    }

    /**
     * 删除关注关系
     *
     * @param currentId 当前登录用户id
     * @param userId    页面用户id
     */
    @DeleteMapping("/{currentId}/{userId}")
    public void deleteRelation(@PathVariable("currentId") Long currentId, @PathVariable("userId") Long userId) {
        User currentUser = userService.getUser(currentId);
        currentUser.setFollowers(currentUser.getFollowers() - 1);//点击关注,登录用户的关注数-1
        userService.updateFansAndFollowersCount(currentUser);//更新数据

        User pageUser = userService.getUser(userId);
        pageUser.setFans(pageUser.getFans() - 1);//点击关注,页面用户的粉丝数-1
        userService.updateFansAndFollowersCount(pageUser);

        userService.deleteRelation(currentId, userId);
    }

    /**
     * 根据用户id查询系统消息
     *
     * @param qo
     * @return
     */
    @GetMapping("/{userId}/systemMessages")
    public PageInfo querySystemMessages(SystemMessageQueryObject qo) {
        qo.setOrderBy("createTime desc");
        PageInfo<?> pageInfo = systemMessageService.query(qo);
        return pageInfo;
    }

    /**
     * 根据用户id查询系统消息的未读数量
     *
     * @param qo
     * @return
     */
    @GetMapping("/{userId}/countUnReadSystemMessages")
    public Object countUnReadSystemMessages(SystemMessageQueryObject qo) {
        JsonResult jsonResult = new JsonResult();
        return jsonResult.setResult(systemMessageService.countUnReadSystemMessages(qo));
    }

    /**
     * 根据用户id查询私信
     *
     * @param qo
     * @return
     */
    @GetMapping("/{acceptId}/letters")
    public PageInfo queryLetterss(LetterQueryObject qo) {
        PageInfo<?> pageInfo = letterService.queryUnreadLetterByUserId(qo);
        return pageInfo;
    }

    /**
     * 根据用户id查询私信未读数量
     *
     * @param qo
     * @return
     */
    @GetMapping("/{acceptId}/countUnReadletters")
    public Object countUnReadletters(LetterQueryObject qo) {
        JsonResult jsonResult = new JsonResult();
        return jsonResult.setResult(letterService.countUnReadletters(qo));
    }

    /**
     * 根据用户id查询点评列表
     *
     * @param qo 用户id
     * @return
     */
    @GetMapping("/{authorId}/strategycomment")
    public PageInfo getStrategyComment(StrategyCommentQueryObject qo) {
        qo.setOrderBy("sc.createTime desc");
        PageInfo comments = strategyCommentService.queryListByAuthorId(qo);
        return comments;
    }

    /**
     * 根据用户id查询点评数量
     *
     * @param authorId
     * @return
     */
    @GetMapping("/{authorId}/strategycommentCount")
    public int strategycommentCount(@PathVariable Long authorId) {
        return strategyCommentService.queryForCount(authorId);
    }

    /**
     * 根据用户id查询点赞我的游记
     *
     * @param qo
     * @return
     */
    @GetMapping("/{userId}/otherslike")
    public PageInfo selectUserAcceptForList(LikeQueryObject qo) {
        qo.setOrderBy("tl.createTime desc");
        PageInfo<?> pageInfo = travelLikeService.selectUserAcceptForList(qo);
        return pageInfo;
    }

    /**
     * 根据用户id查询我点赞的游记
     *
     * @param qo
     * @return
     */
    @GetMapping("/{userId}/userlike")
    public PageInfo selectUserSendForList(LikeQueryObject qo) {
        qo.setOrderBy("tl.createTime desc");
        PageInfo<?> pageInfo = travelLikeService.selectUserSendForList(qo);
        return pageInfo;
    }

    /**
     * 根据用户id查询点赞的游记数
     *
     * @param qo
     * @return
     */
    @GetMapping("/{userId}/countUnReadLikes")
    public Object countUnReadLikes(LikeQueryObject qo) {
        JsonResult jsonResult = new JsonResult();
        return jsonResult.setResult(travelLikeService.countUnReadLikes(qo));
    }

    /**
     * 根据用户id获得点赞 别人的游记总点赞数
     *
     * @param qo
     * @return
     */
    @GetMapping("/{userId}/countLike")
    public Object countLike(LikeQueryObject qo) {
        JsonResult jsonResult = new JsonResult();
        Integer count = travelLikeService.countLike(qo);
        return jsonResult.setResult(count);
    }

    /**
     * 根据用户id查询 获得点赞的游记总点赞数
     *
     * @param qo
     * @return
     */
    @GetMapping("/{userId}/countOthersLike")
    public Object countOthersLike(LikeQueryObject qo) {
        JsonResult jsonResult = new JsonResult();
        Integer count = travelLikeService.countOthersLike(qo);
        return jsonResult.setResult(count);
    }

    /**
     * 根据用户id查询 获得点赞的游记中点赞数最多
     *
     * @param qo
     * @return
     */
    @GetMapping("/{userId}/maxLike")
    public Object maxLike(LikeQueryObject qo) {
        JsonResult jsonResult = new JsonResult();
        return jsonResult.setResult(travelLikeService.maxLike(qo));
    }

    /**
     * 根据用户id查询评论我的评论 或者 游记
     *
     * @param qo
     * @return
     */
    @GetMapping("/{userId}/othercomment")
    public PageInfo othercomment(CommentqueryObject qo) {
        qo.setOrderBy("c.createTime desc");
        PageInfo<?> pageInfo = userService.othercomment(qo);
        return pageInfo;
    }

    /**
     * 根据用户id查询我点评论的一切
     *
     * @param qo
     * @return
     */
    @GetMapping("/{userId}/usecomment")
    public PageInfo usecomment(CommentqueryObject qo) {
        qo.setOrderBy("c.createTime desc");
        PageInfo<?> pageInfo = userService.usecomment(qo);
        return pageInfo;
    }

    /**
     * 根据用户id查询未查看的 所有评论我的 评论或者 游记
     *
     * @param qo
     * @return
     */
    @GetMapping("/{userId}/countUnReadcomment")
    public Object countUnReadcomment(CommentqueryObject qo) {
        JsonResult jsonResult = new JsonResult();
        return jsonResult.setResult(userService.countUnReadcomment(qo));
    }

    /**
     * 根据用户id获得 别人的游记总评论数
     *
     * @param qo
     * @return
     */
    @GetMapping("/{userId}/countComment")
    public Object countComment(LikeQueryObject qo) {
        JsonResult jsonResult = new JsonResult();
        Integer count = travelCommentService.countComment(qo);
        return jsonResult.setResult(count);
    }

    /**
     * 根据用户id查询 获得游记总评论数
     *
     * @param qo
     * @return
     */
    @GetMapping("/{userId}/countOthersComment")
    public Object countOthersComment(LikeQueryObject qo) {
        JsonResult jsonResult = new JsonResult();
        Integer count = travelCommentService.countOthersComment(qo);
        return jsonResult.setResult(count);
    }

    /**
     * 根据用户id查询 获得的游记中评论数最多
     *
     * @param qo
     * @return
     */
    @GetMapping("/{userId}/maxComment")
    public Object maxComment(LikeQueryObject qo) {
        JsonResult jsonResult = new JsonResult();
        return jsonResult.setResult(travelCommentService.maxComment(qo));
    }

    /**
     * 新增订单
     *
     * @param order
     * @return
     */
    @PostMapping("/payOrder")
    public Object payOrder(Order order, Long[] pids) {
        JsonResult jsonResult = new JsonResult();
        orderService.payOrder(order, pids);
        jsonResult.setResultTwo(UserContext.getCurrentUserShopCar());
        jsonResult.setResult(order.getId());
        return jsonResult;
    }

    /**
     * 修改订单状态  有订单号 要修改成的订单状态 1为支付
     *
     * @param qo
     * @return
     */
    @PutMapping("/changeOrderstatus")
    public Object changeOrderstatus(OrderQueryObject qo) {
        JsonResult jsonResult = new JsonResult();
        orderService.changeOrderstatus(qo);
        return jsonResult;
    }

    //根据商品的 数组 和当前登录对象 来 查找下单时的 购物项
    @GetMapping("/productIds")
    public List<ShopCarItem> queryProductsByIds(Long[] pids) {
        return shopCarService.queryProductsByIds(pids);

    }

    //查询当前用户的 订单 的数量 按照 状态来计数
    @GetMapping("/countOrder")
    public List<Map> countOrder() {
        return orderService.countOrder();
    }
    //根据商品的 数组 和当前登录对象 来 查找下单时的 购物项
    @GetMapping("/orderId")
    public Order getOrder(Long orderId){
        return orderService.getOrder(orderId);
    }

}
